<template>
  <div class="Patent-container-padding">
    <el-form :inline="true" :model="formInline" class="form-inline">
      <el-form-item label="专利号：">
        <el-input v-model="formInline.patentCode" />
      </el-form-item>
      <el-form-item label="专利名称：" style="padding: 0">
        <el-input v-model="formInline.patentName" />
      </el-form-item>

      <el-form-item label="申请日期：">
        <el-date-picker
          v-model="formInline.applyDate"
          style="width: 240px"
          type="daterange"
          range-separator="→"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          clearable
        />
      </el-form-item>
      <el-form-item label="完成单位：">
        <el-input v-model="formInline.completeUnit" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="margin-right: 10px"
          @click="handSearch"
        >
          搜索
        </el-button>
        <el-button @click="handReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-divider />
    <div class="buttonList">
      <el-button type="primary" @click="handAddPatent">新增专利</el-button>
      <el-button @click="handExport" :loading="downLoading">
        导出搜索结果
      </el-button>
    </div>
    <vxe-grid v-bind="patentOptions" :height="height">
      <template #operate="{ row }">
        <el-button type="primary" link @click="viewEvent(row, 1)">
          详情
        </el-button>
        <el-button type="primary" link @click="viewEvent(row, 2)">
          修改
        </el-button>
        <el-button type="danger" link @click="del(row)">删除</el-button>
      </template>
      <template #pager>
        <!--使用 pager 插槽-->
        <vxe-pager
          v-model:current-page="tablePage.currentPage"
          v-model:page-size="tablePage.pageSize"
          :layouts="[
            'Sizes',
            'PrevJump',
            'PrevPage',
            'Number',
            'NextPage',
            'NextJump',
            'FullJump',
            'Total',
          ]"
          :total="tablePage.total"
          @page-change="handlePageChange"
        />
      </template>
    </vxe-grid>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import { patentOptions } from './table/column'
  import { useRouter } from 'vue-router'
  import { VxePagerEvents } from 'vxe-table'
  import { getPatentList, deletePatent, getPatentExport } from '@/api/property'
  import { useDownload } from '@/utils/useDownload'

  const router = useRouter()
  // 搜索表单
  const formInline = ref({
    patentCode: '',
    patentName: '',
    completeUnit: '',
    applyDate: '',
  })

  // 导出搜索结果
  const { downLoading, downloadFile } = useDownload(
    getPatentExport,
    '专利列表.xls'
  )
  const handExport = async () => {
    await downloadFile(formInline.value)
  }
  // 搜索
  const handSearch = () => {
    getList()
  }

  // 重置搜索
  const handReset = () => {
    formInline.value = {
      patentCode: '',
      patentName: '',
      completeUnit: '',
      applyDate: '',
    }
    getList()
  }

  const handAddPatent = () => {
    router.push({ path: '/property/patent/add' })
  }

  const viewEvent = (row: any, type: number) => {
    if (type == 1) {
      router.push({ path: '/property/patent/detail', query: { id: row.id } })
    } else if (type == 2) {
      router.push({ path: '/property/patent/add', query: { id: row.id } })
    }
  }

  const tablePage = reactive({
    total: 0,
    currentPage: 1,
    pageSize: 10,
  })

  const handlePageChange: VxePagerEvents.PageChange = ({
    currentPage,
    pageSize,
  }) => {
    tablePage.currentPage = currentPage
    tablePage.pageSize = pageSize
    getList()
  }

  const getList = () => {
    const params = {
      pageNo: tablePage.currentPage,
      pageSize: tablePage.pageSize,
      ...formInline.value,
    }
    getPatentList(params).then((res) => {
      patentOptions.data = res.data.list
      tablePage.total = res.data?.total
    })
  }

  onMounted(() => {
    getList()
  })

  const del = (row: any) => {
    ElMessageBox.confirm('确定删除该条专利?', '删除专利', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      deletePatent(row.id).then((res) => {
        if (res.data) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          getList()
        }
      })
    })
  }
  const height = ref(0)
  interface BaseTableHeight {
    (padding: number): number
  }
  const $baseTableHeight = inject<BaseTableHeight>('$baseTableHeight')
  onBeforeMount(() => {
    window.addEventListener('resize', handleHeight)
  })

  onBeforeUnmount(() => {
    window.removeEventListener('resize', handleHeight)
  })

  const handleHeight = () => {
    height.value = $baseTableHeight(0)
  }
</script>

<style scoped lang="scss">
  .buttonList {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  :deep(.el-divider) {
    margin: 8px 0 20px 0 !important;
  }

  .form-inline {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-column-gap: 20px;
  }
</style>
